package com.javabasic.algorithm.leetcode;

/**
 * @Author xiongmin
 * @Description
 * @Date 2021-03-22 16:31
 * @Version 1.0
 **/
public class NumberOfOneBits {

    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        int tag = 1;
        System.out.println("n = " + n);
        // 先记下最高位的符号位是否为1
        int result = (n < 0 ? 1 : 0);
        int MAX_TAG = 31;
        int temp = 1;
        while (tag < MAX_TAG) {
            result += ((n&temp) > 0 ? 1 : 0);
            System.out.println(((n&temp) > 0 ? temp : temp+"----"));
            temp *= 2;
            tag++;

        }

        return result;
    }
}
